/**
 * @date 2005-12-23 17:15:09
 *
 */
package org.nanhill.xsheet.model;

import java.util.List;

import org.nanhill.commons.model.table.Cell;
import org.nanhill.commons.model.table.Row;
import org.nanhill.commons.model.table.Table;

/**
 * @author alin
 * 
 */
public class XSheetModelHelper {
	public static void transferPushToFixedInEachRow(XSheetModel model) {
		for (Table table : model.getTables()) {
			for (Row row : table.getRows()) {
				List<Cell> cells = row.getCells();
				for (int j = 0; j < cells.size(); j++) {
					Cell cell = cells.get(j);
					if (null != cell) {
						int colspan = cell.getColspan();
						if (colspan > 1) {
							// push cell in this row
							for (int k = 1; k < colspan; k++) {
								cells.add(j + k, null);
							}
							j += colspan - 1;
						}
					}
				}
			}
		}
	}

	public static void transferPushToFixedBetweenRows(XSheetModel model) {
		List<Cell> processRow;
		for (Table table : model.getTables()) {
			List<Row> rows = table.getRows();
			for (int i = 0; i < rows.size(); i++) {
				List<Cell> cells = rows.get(i).getCells();
				for (int j = 0; j < cells.size(); j++) {
					Cell cell = cells.get(j);
					if (null != cell) {
						int rowspan = cell.getRowspan();
						if (rowspan > 1) {
							int colspan = cell.getColspan();
							for (int k = 1; k < rowspan; k++) {
								processRow = table.createRow(i + k).getCells();
								int nowRowSize = processRow.size();
								if (j < nowRowSize) {
									for (int t = 0; t < colspan; t++) {
										processRow.add(j + t, null);
									}
								} else {
									for (int t = nowRowSize; t <= j; t++) {
										processRow.add(t, null);
									}
								}
							}
						}
					}
				}
			}
		}
	}

	public static void transferPushToFixedBetweenRowsIgnoreShortRow(
			XSheetModel model) {
		List<Cell> processRow;
		for (Table table : model.getTables()) {
			List<Row> rows = table.getRows();
			for (int i = 0; i < rows.size(); i++) {
				List<Cell> cells = rows.get(i).getCells();
				for (int j = 0; j < cells.size(); j++) {
					Cell cell = cells.get(j);
					if (null != cell) {
						int rowspan = cell.getRowspan();
						if (rowspan > 1) {
							int colspan = cell.getColspan();
							for (int k = 1; k < rowspan; k++) {
								processRow = table.createRow(i + k).getCells();
								if (j < processRow.size()) {
									for (int t = 0; t < colspan; t++) {
										processRow.add(j + t, null);
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
